package de.justphil.tcg.tcgserver.rest.db.mapstores;


import static de.justphil.tcg.tcgserver.commons.logging.TCGLogging.restLogger;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.TypedQuery;

import org.slf4j.Logger;

import com.hazelcast.core.MapStore;

import de.justphil.tcg.tcgserver.commons.domain.Card;
import de.justphil.tcg.tcgserver.commons.domain.CardInstance;
import de.justphil.tcg.tcgserver.commons.management.Application;
import de.justphil.tcg.tcgserver.rest.db.mapstores.helpers.LoadHelper;

public class CardMapStore implements MapStore<Long, Card> {

	private static final Logger log = restLogger(CardMapStore.class);
	
	private final EntityManagerFactory factory;

	public CardMapStore() {
		log.debug("New object created!");
		factory = Application.getEntityManagerFactory();
	}
	
	@Override
	public Card load(Long cardId) {
		log.debug("load()");
		
		EntityManager em = factory.createEntityManager();
		
		Card card = em.find(Card.class, cardId);
		loadTransientFields(em, card);
		
		em.close();
		
		return card;
	}

	@Override
	public Map<Long, Card> loadAll(Collection<Long> cardIds) {
		log.debug("loadAll()");
		
		EntityManager em = factory.createEntityManager();
		Map<Long, Card> cardMap = new HashMap<Long, Card>();
		
		List<Card> cardList = getCardList(em);
		for(Card card : cardList) {
			
			loadTransientFields(em, card);
			cardMap.put(card.getId(), card);
		}
		
		em.close();
		
		return cardMap;
	}

	@Override
	public Set<Long> loadAllKeys() {
		log.debug("loadAllKeys()");
		
		EntityManager em = factory.createEntityManager();
		
		Set<Long> cardIds = new HashSet<Long>();
		
		List<Card> cardList = getCardList(em);
		for (Card card : cardList) {
			cardIds.add(card.getId());
		}
		
		em.close();
		
		return cardIds;
	}
	
	private void loadTransientFields(EntityManager em, Card card) {
		if (card == null) {
			return;
		}
		
		for (CardInstance ci : card.getCardInstances()) {
			LoadHelper.loadIncludingDecks(em, ci);
		}
	}
	
	private List<Card> getCardList(EntityManager em) {
		TypedQuery<Card> q = em.createQuery("SELECT c FROM Card c ORDER BY c.id", Card.class);
		return q.getResultList();
	}

	@Override
	public void delete(Long cardId) {
		log.debug("delete()");
		
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		
		Card card = em.find(Card.class, cardId);
		if (card != null) {
			em.remove(card);
		}
		
		em.getTransaction().commit();
		em.close();
	}

	@Override
	public void deleteAll(Collection<Long> cardIds) {
		log.debug("deleteAll()");
		
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		
		Card card = null;
		for (Long cardId : cardIds) {
			card = em.find(Card.class, cardId);
			if (card != null) {
				em.remove(card);
			}
		}
		
		em.getTransaction().commit();
		em.close();
	}

	@Override
	public void store(Long cardId, Card card) {
		log.debug("store()");
		
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		
		Card pCard = em.find(Card.class, cardId);
		if (pCard == null) {
			log.debug("em.persist()");
			em.persist(card);
		}
		else {
			log.debug("em.merge()");
			em.merge(card);
		}
		
		em.getTransaction().commit();
		em.close();
	}

	@Override
	public void storeAll(Map<Long, Card> cardMap) {
		log.debug("storeAll()");
		
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		
		Card pCard	= null;
		Card card	= null;
		for (Long cardId : cardMap.keySet()) {
			card = cardMap.get(cardId);
			
			pCard = em.find(Card.class, cardId);
			if (pCard == null) {
				log.debug("em.persist()");
				em.persist(card);
			}
			else {
				log.debug("em.merge()");
				em.merge(card);
			}
		}
		
		em.getTransaction().commit();
		em.close();
	}

}
